package com.example.a79273.shandianban;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a79273.shandianban.adapter.MyAlphaPageTransformer;
import com.example.a79273.shandianban.adapter.StyleGvAdapter;
import com.example.a79273.shandianban.config.NetconfigStyle;
import com.example.a79273.shandianban.model.StyleActivBean;
import com.example.a79273.shandianban.view.MyGridView;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class StyleActivity extends AppCompatActivity implements View
        .OnClickListener {
    private LinearLayout ll_style_scroll, ll_style_color;
    private RelativeLayout rl_style_image;
    private CheckBox cb_style_oppsite;
    private ImageView iv_style_return, iv_style_star, iv_style_color;
    private TextView tv_style_order, tv_style_shopcar;
    private String path;
    private WebView wv_style_clothes;
    private String html;

    private View itemView;
    private List<String> name;
    private HashSet<String> set;
    private List<String> titles;
    private MyGridView gv_style_color;
    private String json;
    private String id;
    private ImageView imag;
    private List<StyleActivBean.DataBean> data;
    private String text;
    private List<String> color;
    private List<String>image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_style);
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {

        iv_style_return.setOnClickListener(this);
        cb_style_oppsite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {


            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked){
                    path = String.format(NetconfigStyle.STYLE_SUB_PATH_Z, id,1);
                    Toast.makeText(StyleActivity.this, path, Toast.LENGTH_SHORT).show();
                }else {

                    path = String.format(NetconfigStyle.STYLE_SUB_PATH_Z, id,2);
                    Toast.makeText(StyleActivity.this, path, Toast.LENGTH_SHORT).show();
                }
                if(data!=null){

                    data.clear();
                }
                if(name!=null){

                    name.clear();
                }
                if(set!=null){

                    set.clear();
                }
                if(color!=null){
                color.clear();
                }
                if(titles!=null){
                titles.clear();}
                if(rl_style_image!=null) {
                    int childCount = rl_style_image.getChildCount();
                    for (int i = 0; i < childCount; i++) {
                        View childAt = rl_style_image.getChildAt(i);
                        rl_style_image.removeView(childAt);
                    }
                }
                if(ll_style_color!=null) {
                    int childCount1 = ll_style_color.getChildCount();
                    for (int i = 0; i < childCount1; i++) {
                        View childAt = ll_style_color.getChildAt(i);
                        ll_style_color.removeView(childAt);
                    }
                }
                if(ll_style_scroll!=null){
                    int childCount = ll_style_scroll.getChildCount();
                    for (int i = 0; i <childCount ; i++) {
                        View childAt = ll_style_scroll.getChildAt(i);
                        ll_style_scroll.removeView(childAt);
                    }
                }
                RequestParams params=new RequestParams(path);
                x.http().get(params, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {

                        json=result;
                        oppsite();
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
            }
        });
    }

    private void setData() {
        RequestParams params = new RequestParams(path);
        x.http().get(params, new Callback.CommonCallback<String>() {



            @Override
            public void onSuccess(String result) {
                json=result;
                oppsite();

            }



            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
        wv_style_clothes.loadDataWithBaseURL(null, html, "text/html", "utf-8",
                null);
    }

    private void initData() {
        titles = new ArrayList<>();
        set = new HashSet<>();
        name = new ArrayList<>();
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        path = String.format(NetconfigStyle.STYLE_SUB_PATH_Z, id,1);
        html = intent.getStringExtra("html");

    }
    private void oppsite() {
        Gson gson = new Gson();
        int start = json.indexOf("[");
        int end = json.lastIndexOf("]");
        String substring = json.substring(start + 1, end);
        StyleActivBean styleActivBean = gson.fromJson(substring,
                StyleActivBean.class);
        data = styleActivBean.getData();
        for (int i = 0; i < data.size(); i++) {
            StyleActivBean.DataBean dataBeanI = data.get(i);
            String nameI = dataBeanI.getName();
            name.add(nameI);
        }
        set.addAll(name);
        titles.addAll(set);
        for (int i = 0; i < titles.size(); i++) {
            text = titles.get(i);
        }
        for (int i = 0; i < titles.size(); i++) {
            String s = titles.get(i);
            itemView = LayoutInflater.from(StyleActivity.this).inflate
                    (R.layout.item_style_scroll, null);
            final TextView tv_style_item_scroll = (TextView) itemView
                    .findViewById(R.id.tv_style_item_scroll);
            tv_style_item_scroll.setText(s);
            ll_style_scroll.addView(itemView);
            tv_style_item_scroll.setOnClickListener(new View
                    .OnClickListener() {


                @Override
                public void onClick(View v) {

                    for (int i = 0; i < ll_style_scroll.getChildCount()
                            ; i++) {


                        TextView view = (TextView) ll_style_scroll
                                .getChildAt(i);
                        view.setTextColor(Color.WHITE);
                        view.setBackgroundColor(Color.BLACK);
                    }
                    tv_style_item_scroll.setTextColor(Color.BLACK);
                    tv_style_item_scroll.setBackgroundColor(Color.WHITE);
                    final String trim = tv_style_item_scroll.getText()
                            .toString().trim();
                    color = new ArrayList<String>();
                    image=new ArrayList<String>();
                    for (int i = 0; i < data.size(); i++) {
                        StyleActivBean.DataBean dataBean = data.get(i);
                        if (trim.equals(dataBean.getName())) {
                            String src = dataBean.getSrc();
                            color.add(src);
                        }
                    }
                    StyleGvAdapter adapter = new StyleGvAdapter(color, StyleActivity.this);
                    gv_style_color.setAdapter(adapter);
                    gv_style_color.setOnItemClickListener(new AdapterView.OnItemClickListener() {


                        @Override
                        public void onItemClick(AdapterView<?> parent,
                                                View view, int position, long id) {

                            String image = color.get(position);
                            for (int i = 0; i <data.size() ; i++) {
                                StyleActivBean.DataBean dataBean = data.get(i);
                                if(image.equals(data.get(i).getSrc())&&trim.equals(dataBean.getName())){
                                    String srcok = data.get(i).getSrcok();
                                    imag = new ImageView(StyleActivity.this);
                                    Picasso.with(StyleActivity.this).load(NetconfigStyle.PIN_JIE+srcok).into(imag);
                                    rl_style_image.addView(imag);

                                }
                            }
                        }
                    });
                }
            });
        }
    }

    private void initView() {
        rl_style_image = (RelativeLayout) findViewById(R.id.rl_style_image);
        gv_style_color = (MyGridView) findViewById(R.id.gv_style_color);
        cb_style_oppsite = (CheckBox) findViewById(R.id.cb_style_oppsite);
        iv_style_return = (ImageView) findViewById(R.id.iv_style_return);
        ll_style_scroll = (LinearLayout) findViewById(R.id.ll_style_scroll);
        ll_style_color = (LinearLayout) findViewById(R.id.ll_style_color);

        iv_style_star = (ImageView) findViewById(R.id.iv_style_star);
        tv_style_order = (TextView) findViewById(R.id.tv_style_order);
        tv_style_shopcar = (TextView) findViewById(R.id.tv_style_shopcar);

        wv_style_clothes = (WebView) findViewById(R.id.wv_style_clothes);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_style_return:
                finish();
                break;
        }
    }

}
